#include "res.h"
#include "manager.h"

    /* stu s[100]={
    {1,"张三",18,'m',9.99},
    {2,"李四",40,'m',45.55},
    {3,"王五",32,'m',34.54},
    {4,"马六",23,'m',67.34}
    };
    int num=4; */

Node* chuang(Stu s){
    Node* pNew=(Node*)malloc(SIZE);
    if(NULL==pNew){
        printf("createNode函数中申请内存失败:%m\n");
        return NULL;
    }
    memset(pNew,0,SIZE);
    pNew->s=s;
    return pNew;
    
}
void sss(Node* list){
    
    printf("%d\t%s\t%d\t%c\t%g\t%g\t%g\t%g\t%2.2lf\t\n",
       list->s.id,list->s.name,list->s.age,list->s.sex,
       list->s.Cscore,list->s.Mscore,list->s.Escore,
       list->s.Cscore+list->s.Mscore+list->s.Escore,
        ((list->s.Cscore + list->s.Mscore + list->s.Escore)/3));
       
}
void paixushow(Node* list){
    printf("学号\t姓名\t年龄\t性别\t语文\t数学\t英语\t总分\t平均分\t排名\t\n");
    if(NULL==list)return;
    int i=1;
    while(list){
        printf("%d\t%s\t%d\t%c\t%g\t%g\t%g\t%g\t%2.2lf\t",
       list->s.id,list->s.name,list->s.age,list->s.sex,
       list->s.Cscore,list->s.Mscore,list->s.Escore,
       list->s.Cscore+list->s.Mscore+list->s.Escore,
        ((list->s.Cscore + list->s.Mscore + list->s.Escore)/3));
        printf("%d\t\n",i++);
        list=list->pNext;
    }
}
void showall(Node* list){
    printf("学号\t姓名\t年龄\t性别\t语文\t数学\t英语\t总分\t平均分\t\n");
   
    if(NULL==list)return;
   
    while(list){
        sss(list);
        
       /*  printf("%d\t%s\t%d\t%c\t%g\t%g\t%g\t%g\t\n",
        list->s.id,list->s.name,list->s.age,list->s.sex,
        list->s.Cscore,list->s.Mscore,list->s.Escore,
        list->s.Cscore+list->s.Mscore+list->s.Escore); */
        list=list->pNext;
    }
}

Stu input (){
    Stu s;
    printf("请输入学生学号:");
    scanf("%d", &(s.id) );

    printf("请输入姓名:");
    scanf("%s",s.name);
 
    printf("请输入年龄:");
    scanf("%d",&(s.age));
    getchar();
    printf("请输入性别:");
    scanf("%c",&(s.sex));

    printf("请输入语文成绩:");
    scanf("%lf",&(s.Cscore));
    printf("请输入数学成绩:");
    scanf("%lf",&(s.Mscore));
    printf("请输入英语成绩:");
    scanf("%lf",&(s.Escore));
    return s;
}




Node* findNode(Node* list){
   /*  int c;
    int n;
    char name[10];
    Node* findnode;
    printf("通过哪方式来找:1.学号   2.姓名\n");
    while(1){
        scanf("%d",&c);
        switch(c){
            case 1: findnode=findNodeById( list ,n);            break;
            case 2: findnode=findNodeByName(list,name[10]);   break;
            default: printf("输入错误，请重新输入!\n");           break;
        }
        if(1==c||2==c)return 0;
    }
    return findnode; */

}
Node* findNodeById(Node* list,int n){
    //异常处理
    if(NULL == list) return NULL;
    
    while(list){
        if(n==list->s.id)return list;
        list=list->pNext;
    }
    return list;
   
}
Node* findNodeByName(Node* list,char name[10]){
    //异常处理
    if(NULL == list) return NULL;
    
    while(list){
        int i=strcmp(name,list->s.name);
        if(0==i)return list;
        list=list->pNext;
    }
    printf(("没有这个人\n"));
    return NULL;
    
   
}


void showone(Node* list,int num){
    system("clear");
    int c;
    Node* s;
    printf("通过哪方式来找:1.学号   2.姓名\n");
    
    while(1){
        scanf("%d",&c);
        if(1==c){
            int b;
            printf("请输入要找的学号:");
            while(1){
                scanf("%d",&b);
                if(b<0||b>num){
                    printf("输入错误，请重新输入！\n");
                }else break;
            
            }
        s=findNodeById(list,b);
        break;
        }else if(2==c){
            printf("请输入要找的姓名:");
            char name [10];
            scanf("%s",name);
            s=findNodeByName(list,name);
            if(NULL==s)return;
            break;
        }else{
            printf("输入错误，请重新输入\n");
        }
    }
   
    
    
    printf("学号\t姓名\t年龄\t性别\t语文\t数学\t英语\t总分\t平均分\t\n");
    sss(s);
}
void paixu(Node** list){
   
    //防呆
    if(NULL==list || NULL==*list || NULL==(*list)->pNext) return ;
    //有序链表
    Node* after=NULL;
    //当前链表头节点
    Node* now=*list;
    //showall(now);
    //当前节点的下一个节点
    Node* next;
   /*  printf("1");
    double nows=now->s.Cscore+now->s.Mscore+now->s.Escore;
    printf("%g",nows);
    double afters=after->s.Cscore+after->s.Mscore+after->s.Escore; */
    while(now){
        next=now->pNext;
        //如果有序链表为空 或者 当前节点的值大于有序链表头结点的值，当前节点变成有序链表头节点
        if(NULL==after||
        //now->s.Cscore > after->s.Cscore
         (now->s.Cscore+now->s.Mscore+now->s.Escore)>
          (after->s.Cscore+after->s.Mscore+after->s.Escore)){
            now->pNext=after;
            after=now;
        }else{
            //否则找到有序链表中应插入的位置
            Node* prev=NULL;
            Node* temp=after;
            //double temps=temp->s.Cscore+temp->s.Mscore+temp->s.Escore;
            
            while(temp && //temp->s.Cscore > now->s.Cscore
             (temp->s.Cscore+temp->s.Mscore+temp->s.Escore)>
           (now->s.Cscore+now->s.Mscore+now->s.Escore)){
                prev=temp;
                temp=temp->pNext;
            }
            now->pNext=temp;
            if(prev)prev->pNext=now;
        }
        //下一个节点
        now=next;
        
        sleep(1);
    }
    //覆盖回来
    *list=after;
    
    paixushow(*list);
   //showall(*list);
}
void load(){
    char key[10]={'0'};
    FILE* fp=fopen("key.txt","r");
    fread(key,1,10,fp);
    system("clear");
    //printf("key:%s\n",key);
    printf("----学生管理系统----\n");
    printf("请输入密码：");
    char a[10];
    int n=0;
    while(1){
    scanf("%s",a);
    
        if(strcmp(a,key)!=0){
            ++n;
            
           if(n>2){
            printf("输入密码错误三次，请10秒后重新试！\n");
            sleep(10);
            system("clear");
            printf("----学生管理系统----\n");
            printf("请输入密码：");
            n=0;
           }
            printf("密码错误，请重新输入:");
        }else{
           // printf("1\n");
          
            break;
        }
    }
    fclose(fp);
    

}
void gaimima(){
    char mima[10]={'0'};
    printf("----学生管理系统----\n");
    printf("请输入新密码:");
    while(1){
        scanf("%s",mima);
        if(strlen(mima)<3){
            printf("密码长度太短，至少输入三位\n");
        }else break;
    }
   
    FILE* fp=fopen("key.txt","w");
    if(NULL==fp)return;
    fputs(mima,fp);
    printf("密码修改成功\n");
    fclose(fp);
}
